package com.zyiot.push;

import java.sql.Date;
import java.text.SimpleDateFormat;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.jpush.android.api.JPushInterface;

import com.zyiot.Dao.MessageDAO;
import com.zyiot.lbs.MainActivity;
import com.zyiot.model.Messages;

/**
 * 自定义接收器
 * 
 * 如果不定义这个 Receiver，则： 1) 默认用户会打开主界面 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
	private static final String TAG = "MyReceiver";
	Context context = null;

	@SuppressWarnings("unused")
	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();
		this.context = context;

		String STR_MESSAGE_MSG = null;
		String STR_MESSAGE_TITLE = null;

		String STR_NOTICE_TITLE;
		String STR_NOTICE_MSG;
		String STR_NOTICE_PATH;

		String STR_NOTICE_URL = null;

		Log.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
		if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
			// 接收自定义消息
			try {
				STR_MESSAGE_MSG = bundle.getString(JPushInterface.EXTRA_MESSAGE);
				String jsonresult = bundle.getString(JPushInterface.EXTRA_EXTRA);

				JSONObject json = new JSONObject(jsonresult);
				if (json != null) {
					STR_NOTICE_URL = json.optString("url");
					STR_MESSAGE_TITLE = json.optString("title");
				}
				if (STR_MESSAGE_MSG != null && !STR_MESSAGE_MSG.equals("")) {
					// 添加到SQLLITE表中
					savedata(STR_MESSAGE_TITLE, STR_MESSAGE_MSG, STR_NOTICE_URL);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
			// 接收自定义通知
			Log.d(TAG, "接收到推送下来的通知:");
			int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
			Log.d(TAG, "接收到推送下来的通知的ID: " + notifactionId);

			STR_NOTICE_TITLE = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
			STR_NOTICE_MSG = bundle.getString(JPushInterface.EXTRA_ALERT);
			STR_NOTICE_PATH = bundle.getString(JPushInterface.EXTRA_RICHPUSH_HTML_PATH);
			Log.d("TEST", "TEST-->" + STR_NOTICE_PATH);
			String jsonresult = bundle.getString(JPushInterface.EXTRA_EXTRA);
			if (STR_NOTICE_MSG != null && !STR_NOTICE_MSG.equals("")) {
				// 添加到SQLLITE表中
				if (STR_NOTICE_PATH != null && !STR_NOTICE_PATH.equals("")) {
					// 富文本地址
					STR_NOTICE_PATH = "file://" + STR_NOTICE_PATH;
					savedata(STR_NOTICE_TITLE, STR_NOTICE_MSG, STR_NOTICE_PATH);
				} else {
					savedata(STR_NOTICE_TITLE, STR_NOTICE_MSG, null);
				}
			}

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
			Log.d(TAG, "用户点击打开了通知");
			 // 打开自定义的Activity
			 Intent _intent = new Intent(context, MainActivity.class);
			 _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			 context.startActivity(_intent);

		} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
			Log.d(TAG, "用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
			// 在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity，
			// 打开一个网页等..

		}
	}

	/**
	 * @param TITLE
	 *            通知标题
	 * @param Message
	 *            通知内容
	 * @param URL
	 *            通知地址
	 **/
	private void savedata(String TITLE, String Message, String URL) {
		Messages message = new Messages();
		MessageDAO messagedao = new MessageDAO(context);
		message.setMsg_title(TITLE);
		message.setMsg_content(Message);
		message.setMsg_url(URL);
		message.setMsg_date(currentDatetime());
		messagedao.add(message);
	}

	private String currentDatetime() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日   HH:mm");
		Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	// 打印所有的 intent extra 数据
	private static String printBundle(Bundle bundle) {
		StringBuilder sb = new StringBuilder();
		for (String key : bundle.keySet()) {
			if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
				sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
			} else {
				sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
			}
		}
		return sb.toString();
	}

}
